package com.thinkfit.jy.utils;

import android.content.Context;
import android.graphics.PixelFormat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.thinkfit.jy.R;

/**
 * CPU使用情况显示（用于在第三方应用中返回本应用）
 */
public class CPUViewUtils implements View.OnTouchListener {

    private static CPUViewUtils backButtonViewUtils;
    WindowManager windowManager;
    Context mContext;
    boolean isFist = true;
    boolean isTounch = false;

    String activityName;
    View inflate;
    int screenWidth,width;
    int showType = 0;
    float posX,posY,curPosX,curPosY;
    WindowManager.LayoutParams layoutParams;

    ConstraintLayout clItem;
    LinearLayout llItem;
    TextView tvTime,tvItemType,tvItem,tvItemUnit;
    float x,y;
    public static CPUViewUtils getInstance(){
        if(backButtonViewUtils == null){
            backButtonViewUtils = new CPUViewUtils();
        }
        return backButtonViewUtils;
    }

    public void setKillPackageName(String name){
        activityName = name;
    }

    public void initView(Context context){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_cpu_button, null);
            DisplayMetrics dm = new DisplayMetrics();
            windowManager.getDefaultDisplay().getMetrics(dm);
//            width = dm.widthPixels;         // 屏幕宽度（像素）
//            System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   width : "+width);
//            float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
//            screenWidth = (int) (width / density);  // 屏幕宽度(dp)

            llItem = inflate.findViewById(R.id.ll_item);
            tvItemType = inflate.findViewById(R.id.tv_item_type);
            tvItem = inflate.findViewById(R.id.tv_item);
            tvItemUnit = inflate.findViewById(R.id.tv_item_unit);


            tvTime = inflate.findViewById(R.id.tv_time);
            clItem = inflate.findViewById(R.id.cl_item);
            clItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goneButton();
                }
            });
            clItem.setOnTouchListener(this);
        }


        if(layoutParams == null){
            layoutParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT
            );

            layoutParams.gravity = Gravity.LEFT| Gravity. TOP;
//            layoutParams.gravity = Gravity.TOP| Gravity. CENTER;
            layoutParams.x = dp2px(190);
        }


        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
        }

    }



    public void setInfo(String info){
        tvItem.setText(info);

    }




    public void goneButton(){
        if(windowManager != null){
            if(inflate != null){
                inflate.setVisibility(View.GONE);
            }
        }

    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                isTounch = true;
                posX = event.getRawX();
                posY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                curPosX = event.getRawX();
                curPosY = event.getRawY();
                x = curPosX - posX;
                y = curPosY - posY;
                posX = curPosX;
                posY = curPosY;
                System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   curPosX : "+curPosX+"   curPosY  : "+curPosY+"   screenWidth : "+screenWidth);
                if(inflate != null){
                    layoutParams.y += Math.round(y);
                    layoutParams.x += Math.round(x);

                    //更新悬浮球控件位置
                    windowManager.updateViewLayout(inflate, layoutParams);
                }
                break;
            case MotionEvent.ACTION_UP:

                if(inflate != null){
//                    if(layoutParams.x < 0){
//                        layoutParams.x = 0;
//                    }else {
//                        layoutParams.x = width;
//                    }

//                    if(curPosX< 100  || curPosX >(width - 100)){
//                        ivBack.setVisibility(View.VISIBLE);
//                        clItem.setVisibility(View.GONE);
//                        if(curPosX< width/2 ){
//                            layoutParams.x = -(width/2);
//                        }else {
//                            layoutParams.x = width/2;
//                        }
//                    }else {
//                        ivBack.setVisibility(View.GONE);
//                        clItem.setVisibility(View.VISIBLE);
//                        layoutParams.y = 0;
//                    }

//                    if(curPosX< width/2 ){
//                        layoutParams.x = -(width/2);
//                    }else {
//                        layoutParams.x = width/2;
//                    }
//                    //更新悬浮球控件位置
//                    windowManager.updateViewLayout(inflate, layoutParams);
                }
                isTounch = false;
                break;
        }
        return false;
    }

    /**
     * dp2px
     */
    protected int dp2px(float dpValue) {
        float density = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }
}
